/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package server;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Slhoka
 */
public class Game {

    private Table table;
    private Users[] users;
    private int nbPlayer;
    private int dealer;

    public Game(Table tb) {
        this.table = tb;
        nbPlayer = table.getNbPlayer();
    }

    public void startGame(int smallBlind)
    {

        int[] bet = new int[nbPlayer];
        
        users = table.getUsers();
        //dealer = users[0].getId();

        System.out.println("Let's play poker");
        System.out.println("----------------------------------------");
        
            System.out.println("Shuffling cards");
                Deck deal = new Deck();
            System.out.println("Dealing cards");

                for(int i = 0; i<= nbPlayer; i++)
                {
                    Hand hand = new Hand(deal, 1);
                    //users[i].setHand(hand);
                }
            System.out.println("Start to bet.");
            bet = bet(smallBlind);

            for(int i = 0; i <= bet.length - 1; i++)
            {
                System.out.println(bet[i]);
            }

    }

    public int[] bet(int smallBlind)
    {
        int nbPlayerBetLeft = nbPlayer;
        int[] bet = new int[nbPlayer];
        
        bet[1] = smallBlind;
        bet[2] = smallBlind * 2;


        BufferedReader clav =new BufferedReader( new InputStreamReader(System.in));
        int x = 0;
        int playerBet = 3;
        int nbBetEgual = 0;
        int maxBet = smallBlind * 2;
        boolean betEgual = false;
        boolean[] fold = new boolean[nbPlayer];
        for(int l = 0; l<= fold.length -1; l++)
        {
            fold[l] = false;
        }

        try {
            while(x<= nbPlayerBetLeft -1 | !betEgual)
            {
                System.out.println("Veuillez parier : \n");
                bet[playerBet] = Integer.parseInt(clav.readLine()) + bet[playerBet];

                if(bet[playerBet] < maxBet)
                {
                    nbBetEgual++;
                    fold[playerBet] = true;
                }
                else if(bet[playerBet] == maxBet)
                {
                    nbBetEgual++;
                }
                else
                {
                    if(playerBet == 0)
                    {
                        if(fold[nbPlayer -1])
                        {
                            int i = 0;
                            boolean out = false;
                            while(i<= bet.length -1 && !out)
                            {
                                if(!fold[nbPlayer - (i+2)])
                                {
                                    out = true;
                                }
                                else
                                {
                                    i++;
                                }
                            }

                            if(bet[playerBet] == bet[nbPlayer - (i+2)])
                            {
                                nbBetEgual++;
                            }

                        }
                        else if(bet[playerBet] == bet[nbPlayer - 1])
                        {
                            nbBetEgual++;
                        }
                    }
                    else if(playerBet == 1)
                    {
                        if(fold[0])
                        {
                            int i = 0;
                            boolean out = false;
                            while(i<= bet.length -1 && !out)
                            {
                                if(!fold[nbPlayer - (i+1)])
                                {
                                    out = true;
                                }
                                else
                                {
                                    i++;
                                }
                            }

                            if(bet[playerBet] == bet[nbPlayer - (i+1)])
                            {
                                nbBetEgual++;
                            }

                        }
                        else if(bet[playerBet] == bet[0])
                        {
                            nbBetEgual++;
                        }
                    }
                    else
                    {
                        if(fold[playerBet - 1])
                        {
                            int i = 0, j = 0;
                            boolean out = false;
                            while(i<= bet.length -1 && !out)
                            {
                                if(playerBet - (j +1) == -1)
                                {
                                    j = 0;
                                }

                                System.out.println("J : " + (j+1));
                                System.out.println("PlayerBet : " + playerBet);
                                if(!fold[playerBet - (j+1)])
                                {
                                    out = true;
                                }
                                else
                                {
                                    i++;
                                    j++;
                                }
                            }

                            if(bet[playerBet] == bet[playerBet - (j+1)])
                            {
                                nbBetEgual++;
                            }
                        }
                        else if(bet[playerBet] == bet[playerBet - 1])
                        {
                            nbBetEgual++;
                        }
                    }
                }
                
                if(playerBet == nbPlayer - 1)
                {
                    playerBet = 0;
                }
                else
                {
                    playerBet++;
                }

                System.out.println("Player Bet : " + playerBet);


                System.out.println("Nb Bet Egual : " +nbBetEgual);

                if(nbBetEgual == nbPlayer)
                {
                    betEgual = true;
                }
                x++;

                System.out.println("X : " + x);
                System.out.println("Bet Egual : " +betEgual);
            }

        } catch (IOException ex) {
            Logger.getLogger(Game.class.getName()).log(Level.SEVERE, null, ex);
        }

        return bet;
    }
}
